package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.FavoriteBatchRequest;
import com.movie.dto.FavoriteRequest;
import com.movie.dto.UserFavoriteDTO;
import com.movie.service.UserFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/favorites")
@RequiredArgsConstructor
@Tag(name = "用户收藏管理", description = "用户收藏电影的增删改查接口")
public class UserFavoriteController {
    
    private final UserFavoriteService userFavoriteService;
    
    @PostMapping
    @Operation(summary = "添加收藏", description = "用户收藏电影")
    public ApiResponse<UserFavoriteDTO> addFavorite(
            @Parameter(description = "收藏请求") @RequestBody FavoriteRequest request) {
        try {
            UserFavoriteDTO favorite = userFavoriteService.addFavorite(request.getUserId(), request.getMovieId());
            return ApiResponse.success("收藏成功", favorite);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @DeleteMapping
    @Operation(summary = "删除收藏", description = "用户取消收藏电影")
    public ApiResponse<Void> removeFavorite(
            @Parameter(description = "删除收藏请求") @RequestBody FavoriteRequest request) {
        try {
            userFavoriteService.removeFavorite(request.getUserId(), request.getMovieId());
            return ApiResponse.success("取消收藏成功", null);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除收藏", description = "用户批量取消收藏电影")
    public ApiResponse<Void> batchRemoveFavorite(
            @Parameter(description = "批量删除收藏请求") @RequestBody FavoriteBatchRequest request) {
        try {
            userFavoriteService.batchRemove(request.getUserId(), request.getMovieIds());
            return ApiResponse.success("批量取消收藏成功", null);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping
    @Operation(summary = "获取用户收藏列表", description = "获取用户的所有收藏电影")
    public ApiResponse<List<UserFavoriteDTO>> getUserFavorites(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            List<UserFavoriteDTO> favorites = userFavoriteService.getUserFavorites(userId);
            return ApiResponse.success(favorites);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/check")
    @Operation(summary = "检查是否收藏", description = "检查用户是否收藏了某部电影")
    public ApiResponse<Boolean> isFavorite(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "电影ID") @RequestParam Long movieId) {
        try {
            boolean isFavorite = userFavoriteService.isFavorite(userId, movieId);
            return ApiResponse.success(isFavorite);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/count")
    @Operation(summary = "统计收藏数量", description = "统计用户的收藏电影数量")
    public ApiResponse<Long> countUserFavorites(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            long count = userFavoriteService.countUserFavorites(userId);
            return ApiResponse.success(count);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
}